package com.eqdd.yiqidian.ui.mine.qiye;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.eqdd.yiqidian.EnterQiyeActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.bean.ComSimpleBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.ui.base.TextSuggestion;
import com.eqdd.yiqidian.ui.mine.unsure.MineAuPerfectActivity;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class EnterQiyeActivity extends BaseActivity {
    private EnterQiyeActivityCustom dataBinding;
    private ArrayList<SearchSuggestion> searchSuggestions;
    private BaseQuickAdapter<ComSimpleBean, BaseViewHolder> quickAdapter;
    private List<ComSimpleBean> comSimpleBeans;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_enter_qiye);
        initTopTitleBar(View.VISIBLE, "入住企业");
    }

    @Override
    public void initData() {

    }

    private void refreshData(String cid) {
        new HttpPresneter.Builder<String>(HttpConfig.GET_COM_SIMPLE_INFO, String.class, this)
                .Params("cid", cid)
                .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Object o) {
                        BaseBean<ComSimpleBean> baseBean = GsonUtils.changeGsonToBaseBean((String) o, ComSimpleBean.class);
                        comSimpleBeans = baseBean.getItems();
                        if (baseBean.getItems() == null || baseBean.getItems().size() <= 0) {
                            ToastUtil.showShort("搜索结果为空");
                        } else if (quickAdapter != null) {
                            ArrayList<ComSimpleBean> temp = new ArrayList<>();
                            temp.addAll(comSimpleBeans);
                            quickAdapter.notifyDataChanged(temp, true);
                        }
                    }
                })
                .build()
                .post();
    }

    @Override
    public void setView() {

        if (quickAdapter == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
            quickAdapter = new BaseQuickAdapter<ComSimpleBean, BaseViewHolder>(R.layout.list_item_06, new ArrayList<>()) {

                @Override
                protected void convert(BaseViewHolder helper, ComSimpleBean item) {

                    helper.setText(R.id.tv_upper_content, item.getComname())
                            .setText(R.id.tv_down_content, item.getComadres());
                }
            };
            dataBinding.recyclerView.setAdapter(quickAdapter);
            if (comSimpleBeans != null) {
                ArrayList<ComSimpleBean> temp = new ArrayList<>();
                temp.addAll(comSimpleBeans);
                quickAdapter.notifyDataChanged(temp, true);
            }
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((rv, position, v) -> {
                        if (user.getComid().equals("0")) {
                            startActivity(MineAuPerfectActivity.class);
                        } else {
                            ToastUtil.showShort("你已拥有另一个企业，不能加入该企业");
                        }
                    });
        }

        dataBinding.floatingSearchView.setOnQueryChangeListener((oldQuery, newQuery) -> {

                    System.out.println(oldQuery + "oldQuery");
                    System.out.println(newQuery + "newQuery");

                    TextSuggestion searchSuggestion = new TextSuggestion();
                    searchSuggestion.setBody(newQuery);
                    searchSuggestions = new ArrayList<>();
                    searchSuggestions.clear();
                    searchSuggestions.add(searchSuggestion);

                    dataBinding.floatingSearchView.swapSuggestions(searchSuggestions);
                }
        );
        dataBinding.floatingSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
            @Override
            public void onSuggestionClicked(SearchSuggestion searchSuggestion) {
                System.out.println("onSuggestionClicked");
                refreshData(searchSuggestion.getBody());

            }

            @Override
            public void onSearchAction(String currentQuery) {
                System.out.println("onSearchAction  " + currentQuery);
            }
        });
        dataBinding.floatingSearchView.setOnBindSuggestionCallback(new SearchSuggestionsAdapter.OnBindSuggestionCallback() {
            @Override
            public void onBindSuggestion(View suggestionView, ImageView leftIcon, TextView textView, SearchSuggestion item, int itemPosition) {

                //here you can set some attributes for the suggestion's left icon and text. For example,
                //you can choose your favorite image-loading library for setting the left icon's image.
                textView.setText(item.getBody());
            }

        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
